package client

import (
	"crawler_distributied/config"
	"crawler_distributied/engine"
	"crawler_distributied/worker"
	"net/rpc"
)

// 单Processor
// func CreateProcessor() (engine.Processor, error) {
// 	client, err := rpcsupport.NewRpcClient(config.WorkerPort0)
// 	if err != nil {
// 		return nil, err
// 	}

// 	return func(req engine.Request) (engine.ParseResult, error) {
// 		sReq := worker.SerializeRequest(req)
// 		var sResult worker.ParseResult

// 		err = client.Call(config.CrawlServiceRpc, sReq, &sResult)
// 		if err != nil {
// 			return engine.ParseResult{}, err
// 		}

// 		return worker.DeserializeResult(sResult), nil
// 	}, nil
// }

func CreateProcessor(clientChan chan *rpc.Client) engine.Processor {
	return func(req engine.Request) (engine.ParseResult, error) {
		sReq := worker.SerializeRequest(req)
		var sResult worker.ParseResult
		ct := <-clientChan
		err := ct.Call(config.CrawlServiceRpc, sReq, &sResult)
		if err != nil {
			return engine.ParseResult{}, err
		}

		return worker.DeserializeResult(sResult), nil
	}
}
